package com.mmall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.dto.PageDto;
import com.mmall.pojo.SkillCategory;
import com.mmall.pojo.User;
import com.mmall.service.ISkillCategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @CLASS SkillCategoryManageController
 * @DESCRIPTION TODO
 * @AUTHor tang
 * @Date 2019-08-23 16:03
 * @VERSION 1.0
 **/
@Controller
@RequestMapping("/manage/skillCategory")
public class SkillCategoryManageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ISkillCategoryService iSkillCategoryService;

    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<PageInfo> list(
            HttpSession session,
            @RequestBody PageDto pageDto
    ){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iSkillCategoryService.list(user.getId(),pageDto.getPageNum(),pageDto.getPageSize());
        }
    }

    @RequestMapping(value = "updateServe",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> updateServe(
            HttpSession session,
            @RequestBody SkillCategory skillCategory
    ){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iSkillCategoryService.updateServe(user.getId(),skillCategory);
        }
    }

    @RequestMapping(value = "delete",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> delete(
            HttpSession session,
            Integer skillCategoryId
    ){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iSkillCategoryService.delete(user.getId(),skillCategoryId);
        }
    }


    @RequestMapping(value = "detail",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<SkillCategory> detail(HttpSession session, Integer skillCategoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iSkillCategoryService.detail(user.getId(),skillCategoryId);
        }
    }
}
